package controllers;

import java.util.List;

import play.modules.paginate.ValuePaginator;
import play.data.validation.Required;
import play.mvc.With;

import common.FBSecure;

import models.CommentTopic;
import models.CommentVideo;
import models.Serie;
import models.Topic;
import models.User;
import models.Video;

@With(FBSecure.class)
public class Videos extends FBController{
	
	public static void videos(long id){		
		
		setFbIdandName();
		
		User user = getUser();
    	Serie series = Serie.findById(id);
		
		List<Video> videos = Video.getAllVideos(id);

        ValuePaginator paginator = new ValuePaginator(videos);
		paginator.setPageSize(5);
		
		render(user, series, videos, paginator);
	}
	
	public static void details(long id, long vid) {
		
		setFbIdandName();
		
		User user = getUser();
    	Serie series = Serie.findById(id);
    	
    	Video video = Video.findById(vid);

        ValuePaginator paginator = new ValuePaginator(video.getCommentVideoList());
		paginator.setPageSize(5);
    	
    	render(user, series, video,paginator);
		
	}
	
	public static void postCommentToVideo(long id, long vid, @Required String comment) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Video video = Video.findById(vid);
        Serie series = Serie.findById(id);

        ValuePaginator paginator = new ValuePaginator(video.getCommentVideoList());
		paginator.setPageSize(5);
		
        if (validation.hasErrors()) {
        	renderTemplate("Videos/details.html", series, video, user, paginator);
		}
        
        user.addCommentToVideo(video, comment);
        user.save();
        
		
        details(id, vid);
    }
    
    
    
    public static void deleteCommentFromVideo(long id, long vid, long cid) {
    	    	
    	User user = getUser();
        CommentVideo comment = CommentVideo.findById(cid);        
        
        if (user.getComments().contains(comment)){
	        user.removeComment(comment);
	        comment.delete();
	        user.save();
        }
		
		details(id, vid);
    }

}
